package com.afishing.dao.impl;

import com.afishing.dao.UserDao;
import com.afishing.entity.User;
import com.afishing.utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        try {
            ResultSet query = JDBCUtil.query("select * from user where username=? and password=?", username, password);
            if (query.next()) {
                return new User(query.getInt("id"), query.getString("username"), query.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean RegisterUser(User user) {
        int update = JDBCUtil.update("insert into user(id,username,password) values(null,?,?)", user.getUsername(), user.getPassword());
        return update > 0;
    }

    @Override
    public void updateUser(User user) {
        JDBCUtil.update("update user set username=?,password=? where id=?", user.getUsername(), user.getPassword(), user.getId());
    }
}
